package com.contest.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.dao.SysMenuDao;
import com.contest.dao.SysRoleMenuDao;
import com.contest.entity.SysMenu;
import com.contest.entity.SysRoleMenu;
import com.contest.service.SysMenuService;
import com.contest.service.SysRoleMenuService;
import com.contest.util.MenuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author yfl
 * @since ${date}
 */
@Service
public class SysMenuServiceImpl extends  ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Autowired
    SysRoleMenuService sysRoleMenuService;
    @Autowired
    SysMenuDao sysMenuDao;

    /**
     * 构建菜单
     *
     * @param fdId 角色id
     * @return
     */
    public List<MenuTree<SysMenu>> buildMenu(String fdId) {
        EntityWrapper<SysRoleMenu> menuEntityWrapper = new EntityWrapper<SysRoleMenu>();
        menuEntityWrapper.addFilter("fd_role_id",fdId);
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.selectList(menuEntityWrapper);
        List<String> menuIds = new ArrayList<String>();
        for (SysRoleMenu roleMenu:roleMenuList){
            menuIds.add(roleMenu.getFdMenuId()+"");
        }
        EntityWrapper<SysMenu> sysMenuEntityWrapper = new EntityWrapper<SysMenu>();
        sysMenuEntityWrapper.in("fd_id",menuIds);
        List<SysMenu> menuList = this.selectList(sysMenuEntityWrapper);
        List< MenuTree<SysMenu>> menuTrees = new ArrayList<MenuTree<SysMenu>>();
        for (SysMenu menu:menuList){
            if(menu.getFdParent().equals("0")){
                MenuTree<SysMenu> menuTree = new MenuTree<SysMenu>();
                menuTree.setName(menu.getFdName());
                menuTree.setUrl(menu.getFdUrl());
                menuTree.setIcon(menu.getFdIcon());
                List<MenuTree<SysMenu>> menuTreeList = buildTree(menu.getFdId(),menuList);
                menuTree.setChildren(menuTreeList);
                menuTrees.add(menuTree);
            }
        }

        return menuTrees;
    }

    @Override
    public Page<SysMenu> selectListByPage(Page<SysMenu> mPage) {
        Map<String,Object> param = mPage.getCondition();
        int rows = mPage.getSize();
        int start = (mPage.getCurrent()-1)*rows;
        List<SysMenu> menuList = sysMenuDao.selectByPage(param,start,rows);
        int total = sysMenuDao.selectByPageCount(start,rows);
        mPage.setRecords(menuList);
        mPage.setTotal(total);
        return mPage;
    }

    private List<MenuTree<SysMenu>> buildTree(String currentId, List<SysMenu> menuList){
        List<MenuTree<SysMenu>> menuTreeList = new ArrayList<MenuTree<SysMenu>>();
        for (SysMenu menu : menuList){
            String id = menu.getFdParent();
            if(id.equals(currentId)){
                MenuTree<SysMenu> menuTree=new MenuTree<SysMenu>();
                menuTree.setIcon(menu.getFdIcon());
                menuTree.setUrl(menu.getFdUrl());
                menuTree.setName(menu.getFdName());
                menuTreeList.add(menuTree);
            }
        }

        return menuTreeList;
    }
}
